﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ObjectManager;

namespace QuanLyDiem
{
    public partial class frmThongTinDangNhap_Sua : Form
    {
        private static frmThongTinDangNhap_Sua frm = null;
        private DangNhap _dangNhap = null;
        private int quyen = 0;
        private string ten = "";

        public event UpdateDangNhap UpdateSuccess = null;

        private frmThongTinDangNhap_Sua()
        {
            InitializeComponent();
            DangNhap dn = new DangNhap();
            cmbQuyen.DataSource = dn.ListName();
        }

        public static frmThongTinDangNhap_Sua Create(DangNhap dangNhap)
        {
            if (frm == null)
            {
                frm = new frmThongTinDangNhap_Sua();
            }
            frm._dangNhap = dangNhap;
            frm.UpdateData();
            return frm;
        }

        private void UpdateData()
        {
            if (_dangNhap != null)
            {
                txtTenDangNhap.Text = _dangNhap.ThongTin.TenDangNhap;
                ten = _dangNhap.ThongTin.Ten;
                txtTen.Text = _dangNhap.ThongTin.Ten;
                cmbQuyen.Text = _dangNhap.ThongTin.TenQuyen;
                quyen = cmbQuyen.SelectedIndex;
            }
        }

        private void frmThongTinDangNhap_Sua_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (frm != null)
            {
                frm.Dispose();
                frm = null;
            }
        }

        private void txtTen_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtTen_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (cmbQuyen.SelectedIndex == quyen && txtTen.Text == ten)
                {
                    btnCancel.PerformClick();
                }
                else
                {
                    btnOk.PerformClick();
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            _dangNhap.ThongTin.Ten = txtTen.Text;
            _dangNhap.ThongTin.TenQuyen = cmbQuyen.Text;
            int check = _dangNhap.Update();
            if (check > 0)
            {
                if (UpdateSuccess != null)
                {
                    UpdateSuccess(this, new InfoUpdateDangNhap{DangNhap = _dangNhap});
                }
                this.Close();
            }
            else
            {
                MessageBox.Show("Đã có vấn đề trong khi sửa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}
